package com.apexedu.eas.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import com.apexedu.eas.entity.Gxxs0101;

import oracle.sql.BLOB;

@Repository
public class Gxxs0101Dao extends BasisDao<Gxxs0101, String> {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	
	public void uploadPhotos(List<String> kshList ,Map<String, File> fileMap) throws IOException{
		 String updatetSql = "update TJX_XSLQ set ZP =? WHERE  KSH = ?";//首先插入一个空的Blob类型
		 String selectSql ="select KSH ,ZP FROM  TJX_XSLQ WHERE  KSH in (  ";
		 String tranSql ="update TJX_XSLQ set ZP =livebosFILE( id ,ksh ,ZP) WHERE  ksh in (";
		 Connection c=null;
		 PreparedStatement ps =null;
		 ResultSet rs =null;
		try {
			 c = SessionFactoryUtils.getDataSource(baseDao.getCurrentSession().getSessionFactory()).getConnection();
			 c.setAutoCommit(false);
			 ps =  c.prepareStatement(updatetSql);
			 for (int i =0 ; i<kshList.size() ;i++) {
				 ps.setBlob(1,BLOB.empty_lob());
				 ps.setString(2,kshList.get(i));
				 ps.addBatch();
				 if(i%1000==0){
	                   ps.executeBatch();
	                   ps.clearBatch();
				 }
			 }
			 ps.executeBatch();
			 ps.clearBatch();
			 closeStatement(ps);
			 
			 log.debug("填入空Blob数据完成！正在修改Blob数据");
			 OutputStream os = null;  
			 
			 StringBuilder queryBuilder = new StringBuilder(selectSql);
			 StringBuilder tranBuilder = new StringBuilder(tranSql);
	           for ( int i = 0; i < kshList.size(); i++) {
	              queryBuilder.append("?");
	              tranBuilder.append("?");
	              if (i != kshList.size() - 1){
                     queryBuilder.append(",");
	              	 tranBuilder.append(",");
	              }
	          }
	          queryBuilder.append(") for update");
	          tranBuilder.append(")");
	         ps = c.prepareStatement(queryBuilder.toString());  
	         for (int i =1 ; i<=kshList.size() ;i++) {
	        	 ps.setString(i, kshList.get(i-1));
	         }
	         rs = ps.executeQuery();  
	         while (rs.next()) {  
	             oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("ZP");  
	             String  ksh = rs.getString("KSH");
	             os = blob.getBinaryOutputStream(0);
	             File f =fileMap.get(ksh);
	             InputStream is = new FileInputStream(f); 
	             byte[] buffer = new byte[1024*2];
	             int len = -1;  
	             while ((len = is.read(buffer)) != -1) {  
	            	 os.write(buffer, 0, len);
	             }  
	             os.flush();  
	             os.close();  
	             is.close();
	         }
	         closeStatement(ps);
	         //转换操作
	         ps = c.prepareStatement(tranBuilder.toString());  
	         for (int i =1 ; i<=kshList.size() ;i++) {
	        	 ps.setString(i, kshList.get(i-1));
	         }
	         ps.executeUpdate();
	         c.commit();
		} catch (SQLException e) {
			try {
				c.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
        closeAll(rs, ps, c);
		
	}
	
	 
	
	
}
